package com.infindo.frame.data.net;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

// URLEncoder for MIDP 1.0
// Based on the source code of java.net.URLEncoder in JDK 1.5.0
// Author: Zetacron

public class URLEncoder {
	private static final int MAX_BYTES_PER_CHAR = 10;

	private static boolean[] dontNeedEncoding;

	static {
		dontNeedEncoding = new boolean[256];

		for (int i = 0; i < 256; i++) {
			boolean b = ((i >= '0') && (i <= '9'))
					|| ((i >= 'A') && (i <= 'Z')) || ((i >= 'a') && (i <= 'z'));

			dontNeedEncoding[i] = b;
		}

		dontNeedEncoding[' '] = true;
		dontNeedEncoding['-'] = true;
		dontNeedEncoding['_'] = true;
		dontNeedEncoding['.'] = true;
		dontNeedEncoding['*'] = true;
	}

	public static String encode(String s, String enc)
			throws UnsupportedEncodingException {
		boolean wroteUnencodedChar = false;

		StringBuffer out = new StringBuffer(s.length());

		ByteArrayOutputStream buf = new ByteArrayOutputStream(
				MAX_BYTES_PER_CHAR);
		OutputStreamWriter writer = new OutputStreamWriter(buf, enc);

		for (int i = 0; i < s.length(); i++) {
			int c = (int) (s.charAt(i));

			if ((c < 128)){//256) && dontNeedEncoding[c]) {
				if (c == ' ') {
					c = '+';
				}

				out.append((char) c);
				wroteUnencodedChar = true;
			} else {
				try {
					if (wroteUnencodedChar) {
						writer = new OutputStreamWriter(buf, enc);
						wroteUnencodedChar = false;
					}

					writer.write(c);

					if (c >= 0xD800 && c <= 0xDBFF) {
						if ((i + 1) < s.length()) {
							int d = (int) (s.charAt(i + 1));

							if (d >= 0xDC00 && d <= 0xDFFF) {
								writer.write(d);
								i++;
							}
						}
					}

					writer.flush();
				} catch (IOException e) {
					buf.reset();
					continue;
				}

				byte[] ba = buf.toByteArray();

				for (int j = 0; j < ba.length; j++) {
					out.append('%');

					char ch = forDigit((ba[j] >> 4) & 0xF, 16);
					out.append(ch);

					ch = forDigit(ba[j] & 0xF, 16);
					out.append(ch);
				}

				buf.reset();
			}
		}
    
		return checkSpace(out.toString());
	}
	
	   private static String checkSpace(String sign){
	    	  int size = sign.length();
	    	  int index = 0;
	    	  char[] tempChar = sign.toCharArray();
	    	  StringBuffer sb = new StringBuffer();
	    	  while(index<size){
	    		  if(tempChar[index]=='+'){
	    			  sb.append("%20");
	    		  }else if(tempChar[index]==' '){
	    			  sb.append("%20");
	    		  }else{
	    		     sb.append(tempChar[index]);
	    		  }
	    		  index++;
	    	  }
	    	  return sb.toString();
	    }

	// based on the source code of java.lang.Character in JDK 1.5.0
	private static char forDigit(int digit, int radix) {
		if ((digit >= radix) || (digit < 0)) {
			return '\0';
		}

		if (digit < 10) {
			return (char) ('0' + digit);
		}

		return (char) ('A' + digit - 10);
	}
}
